$(function () {




    /*开关*/
    $("#switchbutton").switchbutton({
        onText: '充次',
        offText: '充值',
        onChange: function (checked) {
            console.log(checked);
            if(checked){
                $("div[tag='recharge']").hide();
                $("div[tag='rechargeService']").show();
                $("#rechargeServiceitem_datagrid").datagrid('reload');

            }else {
                $("div[tag='rechargeService']").hide();
                $("div[tag='recharge']").show();
                $("#recharge_datagrid").datagrid('reload');

            }
        }
    })
    var status = $("#switchbutton").switchbutton('options').checked;
    if(status){
        $("div[tag='recharge']").hide();
        $("div[tag='rechargeService']").show();
    }else {
        $("div[tag='rechargeService']").hide();
        $("div[tag='recharge']").show();
    }

    /*选择服务下拉框初始化*/
    $("#serviceName").combobox({
        url: '/dataDictionaryItem/query.do?sn=service',
        panelHeight: 'auto',
        valueField: 'name',
        textField: 'name',
        onSelect: function (record) {
            var clientId = $("#clientId2").val();
            $.get("/rechargeServiceItem/getBeforeCount.do", {
                clientId: clientId,
                serviceName: record.name
            }, function (data) {
                if (data) {
                    $("#beforeCount").val(data);
                }
            })
        }
    })

    /*会员列表*/
    $("#allClient_datagrid").datagrid({
        url: "/client/query.do",
        height:510,
        striped: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: "#clientdata_toolbar",
        /*点击行事件*/
        onSelect: function (index, row) {
            /*具体会员信息*/
            ($("#clientTab td span[tag='name']")).html(row.name);
            ($("#clientTab td span[tag='level']")).html(row.level);
            ($("#clientTab td span[tag='balance']")).html(row.balance);
            ($("#clientTab td span[tag='number']")).html(row.number);
            ($("#clientTab td span[tag='birthday']")).html(row.birthday);
            ($("#clientTab td span[tag='integral']")).html(row.integral);
            /*给表单的数据*/
            $("#beforebalance").val(row.balance);
            $("#clientId").val(row.id);
            /*充值明细*/
            $("#recharge_datagrid").datagrid('load', {clientId: row.id})
            /*会员服务列表*/
            $("#clientService").datagrid('load', {clientId: row.id})
            /*充次明细*/
            $("#rechargeServiceitem_datagrid").datagrid('load', {clientId: row.id})
            /*充次表单数据*/
            $("#clientId2").val(row.id);
        },
        columns: [[
            {field: 'name', title: '会员名称', width: 90, align: 'center',styler : function(value, row, index) {
                    return 'border:0;';
                }},
            {field: 'level', title: '等级', width: 90, align: 'center',styler : function(value, row, index) {
                    return 'border:0;';
                }},
            {field: 'sex', title: '性别', width: 90, align: 'center',styler : function(value, row, index) {
                    return 'border:0;';
                }},
            {field: 'tel', title: '电话', width: 180, align: 'center',styler : function(value, row, index) {
                    return 'border:0;';
                }}
        ]]
    })
    /*充值明细列表*/
    $("#recharge_datagrid").datagrid({
        url: "/rechargeItem/query.do",
        fit: true,
        fitColumn: true,
        striped: true,
        pagination: true,
        columns: [[
            {
                field: 'operation', title: '操作', width: 100, align: 'center', formatter: function (value, row, index) {
                    return value == 1 ? "<span style='color: green;font-weight: normal;font-size: unset'>充值</span>" : "<span style='color: red;font-weight: normal;font-size: unset'>消费</span>"
                }
            },
            {field: 'beforeBalance', title: '变动前金额', width: 80, align: 'center'},
            {field: 'income', title: '充值金额', width: 80, align: 'center'},
            {field: 'addincome', title: '赠送金额', width: 80, align: 'center'},
            {field: 'afterBalance', title: '变动后金额', width: 80, align: 'center'},
            {field: 'opUser', title: '操作员', width: 100, align: 'center',formatter: function (value, row, index) {
                    return value ? value.username : '';
                }},
            {field: 'optime', title: '操作时间', width: 180, align: 'center'},
            {field: 'info', title: '备注', width: 230, align: 'center'}
        ]]

    })
    /*充次明细列表*/
    $("#rechargeServiceitem_datagrid").datagrid({
        url: "/rechargeServiceItem/query.do",
        fit: true,
        fitColumn: true,
        striped: true,
        pagination: true,
        columns: [[
            {
                field: 'operation', title: '操作', width: 100, align: 'center', formatter: function (value, row, index) {
                    return value == 1 ? "<span style='color: green;font-weight: normal;font-size: unset'>充值</span>" : "<span style='color: red;font-weight: normal;font-size: unset'>消费</span>"
                }
            },
            {field: 'serviceName', title: '服务名称', width: 80, align: 'center'},
            {field: 'beforeCount', title: '变动前次数', width: 80, align: 'center'},
            {field: 'income', title: '充值金额', width: 80, align: 'center' },
            {field: 'counts', title: '充值次数', width: 80, align: 'center'},
            {field: 'afterCount', title: '变动后次数', width: 80, align: 'center'},
            {field: 'opUser', title: '操作员', width: 100, align: 'center', formatter: function (value, row, index) {
                    return value ? value.username : '';
                }
            },
            {field: 'opTime', title: '操作时间', width: 180, align: 'center'},
            {field: 'info', title: '备注', width: 230, align: 'center'}
        ]]

    })
    /*会员的拥有的服务列表*/
    $("#clientService").datagrid({
        url: "/rechargeServiceItem/getService.do",
        fit: true,
        fitColumn: true,
        striped: true,
        columns: [[
            {field: 'serviceName', title: '服务名称', width: 90, align: 'center'},
            {field: 'counts', title: '次数', width: 90, align: 'center'},
        ]]

    })
    /*高级查询事件*/
    $("#keyword").searchbox({
        searcher: function (value, name) {
            $("#allClient_datagrid").datagrid('load', {
                keyword: value
            })
        }
    })
    /*金额失焦点事件*/
    $("#incomeText").numberbox({
        onChange: function (newValue, oldValue) {
            //赠送金额
            var income = $(this).numberbox('getValue') || 0;
            if (income > 50) {
                $('#addincome').numberbox('setValue', 10);
            } else {
                $('#addincome').numberbox('setValue', 0);
            }

            //合计充值
            var addincome = $("#addincome").numberbox('getValue');
            $("#totalincome").html((parseFloat(addincome) + parseFloat(income)).toFixed(2));
        }

    })
    $("#incomeText2").numberbox({
        onChange: function (newValue, oldValue) {
            //赠送金额
            var income = $(this).numberbox('getValue') || 0;
            if (income > 50) {
                $('#addincome2').numberbox('setValue', 10);
            } else {
                $('#addincome2').numberbox('setValue', 0);
            }

            //合计充值
            var addincome = $("#addincome2").numberbox('getValue');
            $("#totalincome2").html((parseFloat(addincome) + parseFloat(income)).toFixed(2));
        }

    })
})

/*充值提交*/
function save() {
    var id = $("#clientId").val();
    console.log(id);
    if (id==0) {
        $.messager.alert("温馨提示", "请选择会员", "error");
        return;
    }
    $.messager.confirm("温馨提示", "确定充值吗?", function (r) {
        if (r) {
            $("#recharge_form").form('submit', {
                url: "/rechargeItem/recharge.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "充值成功", "info", function () {
                            $("#recharge_form").form('clear');
                            /*清表单数据*/

                            /*刷新明细*/
                            $("#recharge_datagrid").datagrid('reload');
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            })
        }
    })
}

/*充值提交*/
function save2() {
    var id = $("#clientId2").val();
    console.log(id);
    if (id==0) {
        $.messager.alert("温馨提示", "请选择会员", "error");
        return;
    }
    $.messager.confirm("温馨提示", "确定充值吗?", function (r) {
        if (r) {
            $("#rechargeService_form").form('submit', {
                url: "/rechargeServiceItem/recharge.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "充值成功", "info", function () {
                            /*清表单数据*/
                            $("#rechargeService_form").form('clear');
                            /*刷新会员的服务列表*/
                            $("#rechargeServiceitem_datagrid").datagrid('reload');
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            })
        }
    })
}

